package CtrLayer;

import java.util.ArrayList;

import DataLayer.DB.DBKunde;
import DataLayer.Model.Kunde;
import DataLayer.Model.Kunde.MedlemStatus;
import Email.Email;

/**
 * GamingCtr can get all the customers that is set as a Clubmember. it can also notify and send an
 * email to a given or all members.
 * 
 * @author Gruppe 4 - Dennis Biil, Brian Smed, Kristian Henriksen og Anders Engbo
 * @version 1.0
 */
public class GamingCtr implements IFGamingCtr
{
	public GamingCtr()
	{
	}

	public ArrayList<Kunde> getAllKlubmedlem()
	{
		KundeCtr kCtr = new KundeCtr();
		return kCtr.getAllKunder(true);
	}

	public Kunde getKunde(int kundeId)
	{
		KundeCtr kCtr = new KundeCtr();
		Kunde k = kCtr.getKunde(kundeId);
		return k;
	}

	public void updateKlubmedlem(Kunde k)
	{
		KundeCtr kCtr = new KundeCtr();
		kCtr.updateKunde(k);
	}

	/**
	 * Method to count how many active members there are in the club.
	 */
	@Override
	public int getAllActiveKlubmedlem()
	{
		int i = 0;
		for (Kunde k : getAllKlubmedlem())
		{
			if (k.getStatus() == MedlemStatus.ACTIVE || k.getStatus() == MedlemStatus.ENDING)
			{// As long as the status not is INACTIVE the member is an "active" member
				i++;
			}
		}
		return i;
	}

	// sends and Email to every member which status is set to "Ending"
	@Override
	public void notifyAllKlubmedlem(String fromAddress, String subject, String message, String attachFiles[]) throws Exception
	{
		for (Kunde k : getAllKlubmedlem())// for loop to see every member in the club.
		{
			if (k.getStatus() == MedlemStatus.ENDING && k.isUnderrettet() != true)
			{// Only do anything as long as the members status is ending and the member not have
				// been notified yet.
				String toAddress = k.getEmail();
				Email.sendEmailWithAttachments(fromAddress,toAddress, subject, message, attachFiles);
				k.setUnderrettet(true);// sets the member to notified
				DBKunde dbK = new DBKunde();
				dbK.updateKunde(k);
			}
		}
	}

	@Override
	public void sendEmailToAllActiveKlubmedlem(String fromAddress, String subject, String message, String[] attachFiles)
			throws Exception
	{
		for (Kunde k : getAllKlubmedlem())
		{
			if (k.getStatus() == MedlemStatus.ACTIVE || k.getStatus() == MedlemStatus.ENDING)
			{
				String toAddress = k.getEmail();
				Email.sendEmailWithAttachments(fromAddress,toAddress, subject, message, attachFiles);
			}
		}
	}

	@Override
	public void sendEmailToKlubmedlem(String fromAddress, String toAddress,
	                                String subject, String message,
									String[] attachFiles)
			throws Exception
	{
		Email.sendEmailWithAttachments(fromAddress,toAddress, subject, message, attachFiles);
	}

	@Override
	public void printAllActiveKlubmedlem()
	{
		for (Kunde k : getAllKlubmedlem())
		{
			k.getKundeId();
			// TODO add pdf part.
		}
	}

	// gets the members that are soon too expire as a counter
	@Override
	public int getAllExpiringKlubmedlem()
	{
		int i = 0;
		for (Kunde k : getAllKlubmedlem())
		{
			if (k.getStatus() == MedlemStatus.ENDING)
			{
				i++;
			}
		}
		return i;
	}

	// gets the members that have been notified as a counter
	@Override
	public int getAllNotNotifiedKlubmedlem()
	{
		int i = 0;
		for (Kunde k : getAllKlubmedlem())
		{
			if (k.isUnderrettet() == false && k.getStatus() == MedlemStatus.ENDING)
			{
				i++;
			}
		}
		return i;
	}
}
